package com.coco.seck.mqqueue;

import com.coco.common.constant.Constant;
import com.coco.seck.pojo.TbDeploy;
import com.coco.seck.pojo.TbSeckillGoods;
import com.coco.seck.vo.MqDeployVo;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author HanSama
 * @date 2021年07月18日 15:36
 */

@Component
public class AddRedisKillGoodsQueue {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private Redisson redisson;

    /**
     * 队列1
     * @author HanSama
     * @date 2021/7/18 16:40
     * @param deployVo
     */
    @RabbitListener(queues = "addRedisKillGoodsOne")
    @RabbitHandler
    public void  addRedisKillGoodsOne(MqDeployVo deployVo){

        insertRedis(deployVo);
    }

    /**
     * 队列2
     * @author HanSama
     * @date 2021/7/18 16:40
     * @param deployVo
     */
    @RabbitListener(queues = "addRedisKillGoodsTwo")
    @RabbitHandler
    public void  addRedisKillGoodsTwo(MqDeployVo deployVo){

        insertRedis(deployVo);
    }

    /**
     * 队列3
     * @author HanSama
     * @date 2021/7/18 16:40
     * @param deployVo
     */
    @RabbitListener(queues = "addRedisKillGoodsThree")
    @RabbitHandler
    public void  addRedisKillGoodsThree(MqDeployVo deployVo){
        insertRedis(deployVo);
    }


    /**
     * redis添加秒杀商品
     * @author HanSama
     * @date 2021/7/19 10:49
     * @param deployVo
     */
    private void insertRedis(MqDeployVo deployVo) {
        TbSeckillGoods tbSeckillGoods = deployVo.getSeckillGoods();
        TbDeploy deploy = deployVo.getDeploy();
        //秒杀商品的id和数量
        Long seckillId = tbSeckillGoods.getId();
        Integer num = tbSeckillGoods.getStockCount();

        //redis秒杀商品Key
        String seckGood = Constant.RedisKeyConstant.GOODS_KEY_TEMPLATE + seckillId;
        //redisson锁Key
        String addLock = Constant.RedisKeyConstant.GOODS_LOCK_TEMPLATE + seckillId;

        //获取锁
        RLock lock = redisson.getLock(addLock);
        try {
            //上锁
            lock.lock();
            //检查Redis中是否有该商品
            Object o = redisTemplate.opsForValue().get(seckGood);
            if (o==null){
                //将秒杀商品和数量存入redis
                System.out.println("存入Redis");
                HashMap<String, Object> map = new HashMap<>();
                map.put("startTime",deploy.getStartTime());
                map.put("endTime",deploy.getEndTime());
                map.put("startDate",tbSeckillGoods.getStartTime());
                map.put("endDate",tbSeckillGoods.getEndTime());
                redisTemplate.opsForHash().putAll("goods_time:"+seckillId,map);
                redisTemplate.opsForValue().set(seckGood, num.intValue());

            }
        }finally {
            //解锁
            lock.unlock();
        }
    }


}
